package com.itcast.controller;

import com.itcast.pojo.GoodInfo;
import com.itcast.service.IGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feng js
 */
@RestController
@Slf4j
@RequestMapping("/search")
@CrossOrigin("*")
public class GoodsController {
    @Autowired
    IGoodsService goodsService;

    @GetMapping("/goods/{page}/{pageSize}")
    public Map searchByTitle(@RequestParam String keyword,
                             @PathVariable("page")Integer page,
                             @PathVariable("pageSize")Integer pageSize ) {
        Map<String, Object> map = new HashMap<>();
        log.info("开始调用service");
        List<GoodInfo> list = null;
        try {
            list = goodsService.searchByTitle(keyword, page, pageSize);

            map.put("data", list);
            map.put("flag", true);
            map.put("mesg", "访问成功");
          }catch (IOException e) {
            e.printStackTrace();
            map.put("flag", false);
            map.put("mesg", "访问失败");
        }
        return map;
    }
}
